var req;

function buildEventBox(container,elementArray,hiddenArray,isEditable) {
    removeAllNodes(container);
    for(property in elementArray) {
        tmpNodeDiv = document.createElement("div");
        tmpLabelNode = document.createElement("label");
        tmpLabelNodeTxt = document.createTextNode(property + "");
        tmpLabelNode.appendChild(tmpLabelNodeTxt);
        if(isEditable==true) {
            tmpInputBox = document.createElement("input");
            tmpInputBox.value = eval("elementArray." + property + ";");
        } else {
        tmpInputBox = document.createElement("span");
        txtNodeVal = eval("elementArray." + property);
        txtNodeVal = txtNodeVal.replace("_"," ");
        tmpInputBox.appendChild(document.createTextNode(txtNodeVal));
    }
    tmpNodeDiv.appendChild(tmpLabelNode);
    tmpNodeDiv.appendChild(tmpInputBox);
    container.appendChild(tmpNodeDiv);
}
for(property in hiddenArray) {
    tmpInputBox = document.createElement("input");
    tmpInputBox.setAttribute("type","hidden");
    tmpInputBox.value = eval("hiddenArray." + property + ";");
    container.appendChild(tmpInputBox);
}
}

function createEventBox(x,y,elementArray,hiddenArray){
    var anchor = document.getElementById("calendar-event-div");
    var HEIGHT = 150;
    var WIDTH = 430;
    var YTHRESHOLD = 50; // above space from click
    var XTHRESHOLD = 50; // right space from click
    var left,top;
    
    if(!anchor) { return false; }
    var xy;
    xy = getScrollXY();
    SetClientVariables();
    
    top = ((y + xy[1])-HEIGHT) - YTHRESHOLD;
    left = (x + XTHRESHOLD);
    if((left+WIDTH)>CLIENT_WIDTH) {
        left = left - WIDTH - XTHRESHOLD;
    }
    
    var container = document.getElementById("calendar-event-anchor");
    buildEventBox(container,elementArray,hiddenArray,false); // fill in box
    
    anchor.style.width = WIDTH + "px";
    anchor.style.height = HEIGHT + "px";
    anchor.style.top = top + "px";
    anchor.style.left= left + "px";
    
    anchor.style.display="block";
    return false;
}

function closeEventBox(){
    var anchor = document.getElementById("calendar-event-div");
    var HEIGHT = 0;
    var WIDTH = 0;
    if(!anchor) { return false; }
    
    anchor.style.width = WIDTH + "px";
    anchor.style.height = HEIGHT + "px";
    anchor.style.display="none";
    return false;
}

function callbackEvent(x,y){
    if(req.readyState == 4) {
        if(req.status == 200) {
            var response = req.responseText;
            if(response=="") return; // no event by id response
            res = eval(response);
            sDate = res[0].startDate + " " + res[0].startTime;
            eDate = res[0].endDate + " " + res[0].endTime;
            elemArr = {"Title":res[0].title,"Short_Description":res[0].short_description,"Long_Description":res[0].long_description,"Start_Date":sDate,"End_Date":eDate,"Link":res[0].web_link};
            hiddenArr = {"id":res[0].id};
            createEventBox(x,y,elemArr,hiddenArr);
        }
    }
}

function createEventPopup(date) {
    return false;
}

function showEvent(id,event) {
    var url = "/calendar/event/" + id + "/" + rand();
    if(window.event) {
        x = window.event.x;
        y = window.event.y;
    } else {
    x = event.clientX;
    y = event.clientY;
}
if(window.XMLHttpRequest) {
    req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.onreadystatechange = function() { callbackEvent(x,y) };
req.send(null);
}

function navigate(month,year) {
    var url = "/calendar/display/"+month+"/"+year + "/" + rand();
    if(window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}

function callback() {
    obj = document.getElementById("calendar");
    //setFade(0);
    if(req.readyState == 4) {
        if(req.status == 200) {
            response = req.responseText;
            obj.innerHTML = response;
            //fade(0);
        } else {
        alert("There was a problem retrieving the data:\n" + req.statusText);
    }
}
}

function fade(amt) {
    if(amt <= 100) {
        setFade(amt);
        amt += 10;
        setTimeout("fade("+amt+")", 5);
    }
}

function setFade(amt) {
    obj = document.getElementById("calendar");
    amt = (amt == 100)?99.999:amt;
    // IE
    obj.style.filter = "alpha(opacity:"+amt+")";
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = amt/100;
    // Mozilla and Firefox
    obj.style.MozOpacity = amt/100;
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = amt/100;
}